const { body } = require('express-validator')
const validate = require('../middleware/validate')
const { User } = require('../model')
const { phoneAndCode } = require('../controller/message')
const md5 = require('../util/md5')

exports.register = validate([
  body('user.username')
    .notEmpty().withMessage('用户名不能为空')
    .custom(async username => {
      const user = await User.findOne({ username })
      if (user) {
        return Promise.reject('用户名已存在')
      }
    }),

  body('user.phone')
  .notEmpty().withMessage('手机号码不能为空')
  .bail()
  .custom( async (phone, { req }) => {
    const userPhone = await User.findOne({ phone })
    if (userPhone) {
      return Promise.reject('手机号已注册')
    }
    const isPhone = phoneAndCode.find(item => String(item.phone) === String(phone))
    console.log(phoneAndCode)
    if(!isPhone){
      return Promise.reject('请先获取验证码')
    }
    req.isPhone = isPhone
  }),

  body('user.code')
  .notEmpty().withMessage('验证码不能为空')
  .bail()
  .custom( async ( code, { req } ) => {
    console.log(req)
    if(Number.parseInt(req.isPhone.code) !== Number.parseInt(code)){
      return Promise.reject('验证码不正确或已过期')
    }
  }),

  body('user.password').notEmpty().withMessage('密码不能为空'),

  body('user.email')
    .notEmpty().withMessage('邮箱不能为空')
    .isEmail().withMessage('邮箱格式不正确')
    .bail()
    .custom(async email => {
      const user = await User.findOne({ email })
      if (user) {
        return Promise.reject('邮箱已存在')
      }
    })
])

// 普通登录验证
exports.login = [
  validate([
    body('user.email').notEmpty().withMessage('邮箱不能为空'),
    body('user.password').notEmpty().withMessage('密码不能为空')
  ]),
  validate([
    body('user.email').custom(async (email, { req }) => {
      const user = await User.findOne({ email })
        .select(['email', 'username', 'bio', 'image', 'password', 'phone'])
      if (!user) {
        return Promise.reject('邮箱不存在')
      }
      // 将数据挂载到请求对象中，后续的中间件也可以使用了
      req.user = user
    })
  ]),
  validate([
    body('user.password').custom(async (password, { req }) => {
      if (md5(password) !== req.user.password) {
        return Promise.reject('密码错误')
      }
    })
  ])
]

// 短信登录验证
exports.message = [
  validate([
    body('user.phone')
    .notEmpty().withMessage('手机号码不能为空')
    .bail()
    .custom( async (phone, { req }) => {
      const user = await User.findOne({ phone })
        .select(['email', 'username', 'bio', 'image', 'password', 'phone'])
      if (!user) {
        return Promise.reject('手机号码不存在')
      }
      req.user = user
    })
  ]),
  validate([
    body('user.code')
    .notEmpty().withMessage('验证码不能为空')
    .bail()
    .custom( async ( code, { req } ) => {
      // 判断当前是否存在已发送短信验证码的手机号
      const isPhone = phoneAndCode.find(item => String(item.phone) === String(req.user.phone))
      if(!isPhone) {
        return Promise.reject('验证码不正确')
      }
      if(Number.parseInt(isPhone.code) !== Number.parseInt(code)){
        return Promise.reject('验证码不正确或已过期')
      }
    })
  ])
]
